import { Module } from "vuex";
import { ISystemStateType, IGetPageListActionPaylodType } from "./type";
import { IRootState } from "@/store/type";
import { getPageList } from "@/service/main/system/system";
import { IUserListType } from "@/service/main/system/type";

const systemModule: Module<ISystemStateType, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0
    };
  },
  getters: {
    pageTableDataGetter(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
        /* switch (pageName) {
          case "users":
            return state.usersList;
          case "role":
            return state.roleList;
        } */
      };
    },
    pageTableCountGetter(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`];
      };
    }
  },
  mutations: {
    changeUsersList(state, usersList: IUserListType[]) {
      state.usersList = usersList;
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount;
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList;
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount;
    },
    changeGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList;
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount;
    },
    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList;
    },
    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount;
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: IGetPageListActionPaylodType) {
      const pageName = payload.pageName;
      const url = `${pageName}/list`;
      const res = await getPageList(url, payload.queryInfo);
      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1);
      commit(`change${changePageName}List`, res.data.list);
      commit(`change${changePageName}Count`, res.data.totalCount);
      /* switch (pageName) {
        case "users":
          commit("changeUserList", res.data.list);
          commit("changeUsersCount", res.data.totalCount);
          break;
        case "role":
          commit("changeRoleList", res.data.list);
          commit("changeRoleCount", res.data.totalCount);
      } */
    }
  }
};

export default systemModule;
